(function() {
    'use strict';

    /**
     * @ngdoc directive
     * @name webApp.directive:loginDialog
     * @description
     * # loginDialog
     */
    angular.module('webApp')
        .run(["$templateCache", "dataService",
            function($templateCache, dataService) {
                var tplName = "views/directives.kanjia.html";
                dataService.tpl(tplName).then(function(html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive('kanjia', ["$rootScope", "helper", function($rootScope, helper) {
            return {
                restrict: 'EA',
                template: "<span><a href='javascript:;' class='sui-btn btn-link' ng-click='showPro()'>+ {{'选择砍价商品'|translate}}</a>",
                require:'ngModel',
                scope: {
                    ngModel:"=",
                    callback:"&",
                },
                controller: ['$scope', 'authService', 'sessionService', 'dataService', '$compile', 'helper', '$templateCache','$timeout', function($scope, authService, sessionService, dataService, $compile, helper, $templateCache,$timeout) {
                    $scope.productcat={};
                    $scope.showPro=function(){
                        $scope.initData().then(showSelect)
                    }
                    $scope.initData=function(pageNo){
                        pageNo=pageNo?pageNo:1;
                        $scope.page_size=10;
                        var user = sessionService.get("tokenUser");

                        var query = { page_no: pageNo ? pageNo : "1", page_size: $scope.pageSize };
                        query.query = { "partner_id": user.CompanyId };
                       return dataService.kanjiaGet(query).then(function(r){
                            if(r.Msg){

                            }else{
                              $scope.bargain_rules=r.bargain_rules;
                              $scope.bargain_rules_total_count = r.total_count;

                            }
                        });
                    }
                    $scope.addKnaji=function(kanjiaObj){
                        $scope.kanjiaObj=kanjiaObj;
                    }
                    this.closeWin = function() {
                        loadingToggle(true);
                        if ($scope.loginWin) {
                            $scope.loginWin.modal("hide");
                        }
                    }
                     function showSelect() {
                        $scope.error = null;
                        //弹出
                      $.alert({
                            backdrop: "static",
                            keyboard: true,
                            title: '{{"need_login"|translate}}',
                            body: $templateCache.get("views/directives.kanjia.html"),
                            okBtn: '{{"login"|translate}}',
                            closeBtn: true,
                            transition: true,
                            width: 'large',
                            height:300,
                            show: function(e) {
                                $compile($(e.currentTarget).contents())($scope);
                            },
                            okHide: function(e) {
                                $timeout(function(){
                                    if(_.size($scope.kanjiaObj)>0){
                                    	console.log("fenxiao",$scope.kanjiaObj);
                                        $scope.callback({bargain_rule_id:$scope.kanjiaObj.bargain_rule_id});
                                    }
                                })
                            },
                            shown: function(e) {
                            }
                        });
                    }
                    $scope.initProductcat=function(id){
                        console.log(id);
                        dataService.fenxiaogradeOne(id).then(function(fenxiaograde){
                            $scope.fenxiaograde=fenxiaograde;
                        }); 
                    }
                }],
                link: function(scope, elem, attrs, ctr) {
                     scope.$watch('ngModel', function(newValue, oldValue, scope) {
                        console.log(newValue,oldValue);
                        if(!_.isUndefined(newValue)&&newValue!=0){
                            scope.initProductcat(newValue);
                        }
                        // ctr.$setViewValue(newValue);
                        // scope.chosedItems = newValue;
                    });
                }
            }
        }]);
})();
